import java.util.LinkedList;
import java.util.Queue;

/**
 * @author gao
 * @date 2025/7/29 17:36
 */

class MyStack {

    public Queue<Integer> q1;
    public Queue<Integer> q2;

    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }

    public void push(int x) {
        if (empty()) {
            q1.offer(x);
            return;
        }
        if (!q1.isEmpty()) {
            q1.offer(x);
        } else {
            q2.offer(x);
        }
    }

    public int pop() {
        if (empty()) {
            return -1;
        }
        if (!q1.isEmpty()) {
            int len = q1.size();
            while (len - 1 != 0) {
                q2.offer(q1.poll());
                len--;
            }
            return q1.poll();
        } else {
            int len = q2.size();
            while (len - 1 != 0) {
                q1.offer(q2.poll());
                len--;
            }
            return q2.poll();
        }
    }

    public int top() {
        if (empty()) {
            return -1;
        }
        if (!q1.isEmpty()) {
            int len = q1.size();
            int tmp = -1;
            while (len != 0) {
                tmp = q1.poll();
                q2.offer(tmp);
                len--;
            }
            return tmp;
        } else {
            int len = q2.size();
            int tmp = -1;
            while (len != 0) {
                tmp = q2.poll();
                q1.offer(tmp);
                len--;
            }
            return tmp;
        }
    }

    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();
    }
}

public class Solution {
    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.top());
        System.out.println(stack.pop());
        System.out.println(stack.empty());
    }
}